#include "jshandler.h"

JSHandler* JSHandler::parser=nullptr;
QHash<QString, QByteArray> JSHandler::handlers=QHash<QString, QByteArray>();
QHash<QString,QByteArray> JSHandler::respondhash=QHash<QString,QByteArray>();

JSHandler::JSHandler(){}

void JSHandler::dealSignup(const QString &status)
{
    qDebug()<<"emit "<<status;
    if(status=="pass")
    {
        emit s_signUpPass();
    }
    else if(status=="error")
    {
        emit s_signUpError();
    }
    else
    {
        qDebug()<<"unknown status";
    }
}

void JSHandler::dealLogin(const QString &status)
{
    qDebug()<<"emit "<<status;
    if(status=="pass")
    {
        emit s_logInPass();
    }
    else if(status=="error")
    {
        emit s_logInError();
    }
    else if(status=="ban")
    {
        emit s_logInBan();
    }
    else
    {
        qDebug()<<"unknown status";

    }
}

void JSHandler::fileReady(const QJsonObject &rootobj)
{
    Q_UNUSED(rootobj);
    emit s_fileReady();
}

void JSHandler::analysisScfilehead(const QJsonObject &rootobj)
{
    QJsonObject contentobj=rootobj.value("content").toObject();
    QString senderoc=contentobj.value("senderoc").toString();
    unsigned long filesize=contentobj.value("filesize").toVariant().toULongLong();
    QString filesha256=contentobj.value("filesha256").toString();

    QString filename=contentobj.value("filename").toString();


    emit s_recvFileHead(senderoc,filesize,filesha256,filename);
}

void JSHandler::fileEnd(const QJsonObject &rootobj)
{
    emit s_fileEnd();
}

JSHandler *JSHandler::getInstance()
{
    if(!parser)
    {
        parser=new JSHandler;
        handlers["csmessage"] = "analysisMessage";
        handlers["respond"] = "analysisRespond";
        handlers["fileready"]="fileReady";
        handlers["scfilehead"]="analysisScfilehead";
        handlers["fileend"]="fileEnd";

        respondhash["signup"]="dealSignup";
        respondhash["login"]="dealLogin";
    }
    return parser;
}

void JSHandler::handle(const QJsonDocument& doc)
{
    QJsonObject rootobj=doc.object();
    QString type=rootobj.value("type").toString();
    if (handlers.contains(type)) {
        parser->metaObject()->invokeMethod(parser, handlers[type].data(), Q_ARG(const QJsonObject&,rootobj));
    } else {
        qDebug() << "Unknown type:" << type;
    }
}


void JSHandler::analysisMessage(const QJsonObject &rootobj)
{
    QJsonObject contentobj=rootobj.value("content").toObject();
    QString targetoc=contentobj.value("targetoc").toString();
    QString message=contentobj.value("message").toString();
    qDebug()<<targetoc<<message;
    emit s_haveNewMessage(QPair<QString,QString>(targetoc,message));
}

void JSHandler::analysisRespond(const QJsonObject &rootobj)
{
    QJsonObject contentobj=rootobj.value("content").toObject();
    QString subtype=contentobj.value("subtype").toString();
    QString status=contentobj.value("status").toString();
    if(respondhash.contains(subtype))
    {
        parser->metaObject()->invokeMethod(parser, respondhash[subtype].data(), Q_ARG(const QString&,status));
    }
    else {
        qDebug() << "Unknown type:" << subtype;
    }
}

